package com.kk.my_pro.config.security;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kk.my_pro.entity.User;
import com.kk.my_pro.mapper.UserMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsPasswordService;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.provisioning.UserDetailsManager;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;

@Component
@Slf4j
public class DBUserDetailsManager  implements
//        UserDetailsManager,
//        UserDetailsPasswordService
        UserDetailsService
{
    @Resource
    private UserMapper userMapper;

//    @Override
//    public UserDetails updatePassword(UserDetails user, String newPassword) {
//        return null;
//    }
//
//    /**
//     * 向数据库中插入用户数据
//     * @param userDetails
//     */
//    @Override
//    public void createUser(UserDetails userDetails) {
//        User user = new User();
//        user.setPassword(userDetails.getPassword());
//        userMapper.insert(user);
//    }
//
//    @Override
//    public void updateUser(UserDetails user) {
//
//    }
//
//    @Override
//    public void deleteUser(String username) {
//
//    }
//
//    @Override
//    public void changePassword(String oldPassword, String newPassword) {
//
//    }
//
//    @Override
//    public boolean userExists(String username) {
//        return false;
//    }




    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LambdaQueryWrapper<User> userQueryWrapper = new LambdaQueryWrapper<>();
        userQueryWrapper.eq(User::getUsername,username);
        User user = userMapper.selectOne(userQueryWrapper);

        if(user==null){
            throw new UsernameNotFoundException(username);
        }else {
//            Collection<GrantedAuthority> authorities = new ArrayList<>(); // 该用户角色列表
//            // 测试硬编码
//            authorities.add(()->"USER_LIST");
//            authorities.add(()->"USER_ADD");
//            return new org.springframework.security.core.userdetails.User(
//                    user.getUsername(),
//                    user.getPassword(),
//                    user.getEnabled(),
//                    true, //用户账号是否过期
//                    true, // 用户凭证是否过期
//                    true, //  用户是否未被锁定
//                    authorities //权限列表
//            );


            // 测试权限硬编码
//            return org.springframework.security.core.userdetails.User
//                    .withUsername(user.getUsername())
//                    .password(user.getPassword())
//                    .credentialsExpired(false)
//                    .accountLocked(false)
//                    .roles("ADMIN") // 硬编码给改用户赋予ADMIN角色权限
//                    .build();



            return user;

        }
    }





    public User insertUser(User user){
        try {
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(User::getUserName, user.getUserName());
            if (userMapper.selectList(wrapper).size() == 0){
                userMapper.insert(user);
                return user;
            }else {
                log.error("userName already exists");
                return null;
            }
        }catch (Exception e){
            log.error(e.getMessage());
            return null;
        }
    }

}
